package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import java.util.Vector;
import com.sound.ampache.objects.Tag;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;


class TagsScreen extends MainScreen implements HTTPTransportListener{

	private Tag[] tags;
	private TagsListField tagsList;
	private Dialog statusDlg;
	private ArtistsScreen artistsScreen;
	private AlbumsScreen albumsScreen;
	private SongsScreen songsScreen;
	public String screenTitle = "Tags";
	
    private MenuItem viewArtistsMenu = new MenuItem("View Artists",100,10) {
        public void run() {
            viewArtists();
        } 
     };
     
     private MenuItem viewAlbumsMenu = new MenuItem("View Albums",100,10) {
         public void run() {
             viewAlbums();
         } 
      };
	
	private MenuItem viewSongsMenu = new MenuItem("View Songs",100,10) {
        public void run() {
            viewSongs();
        } 
     };
     
     private MenuItem addSongsMenu = new MenuItem("Add Songs to Playlist",100,10) {
         public void run() {
             addSongs();
         } 
      };
	
	TagsScreen() {
        setTitle( new LabelField( screenTitle , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        tagsList = new TagsListField();
        tagsList.setEmptyString("No Tags", DrawStyle.HCENTER);
        this.add(tagsList); 
        Bitmap icon = Bitmap.getBitmapResource("img/download.png"); 
        statusDlg = new Dialog("Downloading...",null,null,0,icon); 
	}
	
	
	public void getTags() {
		getTags("tags", "");
	}
	
	
	public void getTags(String filter) {
		getTags("tags", filter);
	}
	
	
	public void getTags(String method, String filter){
		setTitle(screenTitle);
		tagsList.set(null); 
		statusDlg.show();
		
		try {
	        ampacheberry.comm.setHTTPTransportListener(this);
	        ampacheberry.comm.send(method, filter); 
	
		} catch (Exception e1) {
			e1.printStackTrace();
		}

	}
	
	
	public void processResponse(final Vector data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
                statusDlg.close();
    			tags = new Tag[data.size()];
    			data.copyInto(tags);
    			tagsList.set(tags);
            }
        });        
	}
	
	
    protected void makeMenu(Menu menu, int instance) {
    	menu.add(viewArtistsMenu);
    	menu.add(viewAlbumsMenu);
        menu.add(viewSongsMenu);
        menu.add(addSongsMenu);
        menu.add(MenuItem.separator(addSongsMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
    
	
    private class TagsListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/tag.png");  
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     
                    
            if (null != icon) {
                int offsetY = (this.getRowHeight() - icon.getHeight())/2;
                graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                        icon.getHeight(),icon,0,0);
                graphics.drawText(tags[index].name + " (" + tags[index].albums + ")", 
                    icon.getWidth() + 2, y, DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            } else {
                graphics.drawText("- " + tags[index].name + " (" + tags[index].albums + ")", 0,
                    y, DrawStyle.ELLIPSIS, width - graphics.getFont().getAdvance("- "));
            }
        }
    };
    
    
    private void viewArtists() {
    	int selected = tagsList.getSelectedIndex();
    	
    	if (null == artistsScreen) {
    		artistsScreen = new ArtistsScreen();
    	}
    	artistsScreen.setRowHeight();
    	artistsScreen.screenTitle = "Artists - " + tags[selected].name;
    	UiApplication.getUiApplication().pushScreen(artistsScreen);
    	artistsScreen.getArtists("tag_artists", tags[selected].id);
    }
    
    
    private void viewAlbums() {
    	int selected = tagsList.getSelectedIndex();
    	
    	if (null == albumsScreen) {
    		albumsScreen = new AlbumsScreen();
    	}
    	albumsScreen.setRowHeight();
    	albumsScreen.screenTitle = "Albums - " + tags[selected].name;
    	UiApplication.getUiApplication().pushScreen(albumsScreen);
        albumsScreen.getAlbums("tag_albums", tags[selected].id);
    }
    
    
    private void viewSongs() {
    	int selected = tagsList.getSelectedIndex();
    	
    	if (null == songsScreen) {
    		songsScreen = new SongsScreen();
    	}
    	songsScreen.setRowHeight();
    	songsScreen.screenTitle = "Songs - " + tags[selected].name;
    	UiApplication.getUiApplication().pushScreen(songsScreen);
        songsScreen.getSongs("tag_songs", tags[selected].id);
    }
    
    
    private void addSongs() {
    	int selected = tagsList.getSelectedIndex();
    	ampacheberry.playlistSong.addTagToPlaylist(tags[selected]);
    }
    
} 
